/*
 * main.cpp
 *
 *  Created on: 07.11.2011
 *      Author: delgado
 */
#include "TIDA.h"
using TIDA::TIDAFile;
#include "commandParser.h"
using TIDA::commandOptions;
#include <iostream>
using std::cout;
using std::endl;
using std::cerr;
#include "error.h"

void creditsText(void)
{
	cout << "TIDA to TEXT version 10e-9 BETA (" << __DATE__ << ")" << endl;
	cout << "Only for testing purposes! Free Distribution" << endl;
	cout << "Written by Ignacio Delgado" << endl;
	cout << "bugs, annoyances, complains and suggestions to" << endl;
	cout << "\tignacio.delgado@mdc-berlin.de" << endl;
	cout << "THIS SOFTWARE COMES WITH ABSOLUTELY NO WARRANTY! USE AT YOUR OWN RISK!" << endl;
	cout << endl;
}



int main(int argc, char* argv[])
{
	bool verbose =false;
	bool exit=false;
	try
	{
		commandOptions cmd(argc, argv);
		if (cmd.help)
		{
			creditsText();
			return 0;
		}
//		cout << cmd << endl;  // Uncomment for debugging command options
		if (cmd.verbose)
		creditsText();
		verbose=cmd.verbose;
		
		TIDAFile tf(cmd.inputFile, cmd.verbose);

		if (cmd.header)
		{
			tf.headerInfo();
			exit=true;
		}
		if (cmd.recpar)
		{
			tf.recparInfo();
			exit=true;
		}
		if (cmd.scalpar)
		{
			tf.scalparInfo();
			exit=true;
		}
		if (cmd.comments)
		{
			tf.commentsInfo();
			exit=true;
		}
		if (cmd.amplpar>=0)
		{
			tf.amplparInfo(cmd.amplpar);
			exit=true;
		}

		if (cmd.fileStructure)
		{
			tf.viewFileStructure();
			exit=true;
		}
		
		if (exit) return 0; // Information option will exit without file writting
		tf.toASCII(cmd.outputFile, cmd.forceOverwrite, cmd.verbose);
	}
	catch (neurignacio::ErrorCode& e)
	{
		if (!verbose)
			creditsText();
		cout << "Program execution aborted due to an error. I guess the user was a PhD student..." << endl;
		cout << "Use " << string(argv[0]) << " --help for assistance or hire another PhD student" << endl;
		return -1*e; // Error codes will be returned as negative values
	}
	return 0;
}


// Test file: "/home/delgado/Data/TIDA_data/Daniel/DH100802.016"
